package puzzle.projecteuler.p200;


public class Problem142 {

	/**
	 * 		x + y = m^2 y + z = n^2, z + x = k^2
	 * => 	x = ( m^2 + k^2 - n^2)/2
	 * 		y = ( m^2 - k^2 + n^2)/2
	 * 		z = (-m^2 + k^2 + n^2)/2
	 * 并且:x - y = k^2 - n^2
	 * 		y - z = m^2 - k^2
	 * 		x - z = m^2 - n^2
	 * 其中, m > k > n 且 2| (m+k+n)
	 * 
	 * answer	: 1006193
	 * time cost: 31 ms
	 * @param args
	 */
	public static void main(String[] args) {
		
		long s = System.currentTimeMillis();
		
		long x = -1, y = -1, z = -1;
		for (int m = 3; m < 1000; m ++) {
			for (int k = 2; k < m; k ++) {
				if (isSquare(m*m - k*k)) {
					for (int n = 1; n < k; n ++) {
						if ((m+n+k)%2 == 0 && isSquare(k*k - n*n) && isSquare(m*m - n*n)) {
							x = ( m*m + k*k - n*n)/2;
							y = ( m*m - k*k + n*n)/2;
							z = (-m*m + k*k + n*n)/2;
							break;
						}
					}
				}
			}
		}
		System.out.println(x+y+z);
		System.out.println((System.currentTimeMillis()-s) + " ms");
	}
	
	private static boolean isSquare(int m) {
		
		int t = (int)Math.sqrt(m);
		return t*t == m;
	}
}
